from typing import Any, Generic, Optional, TypeVar

from pydantic import BaseModel, Field

T = TypeVar("T")

__all__ = ["CommonResp"]


class CommonResp(BaseModel, Generic[T]):
    code: int = Field(0, description="0表示正常, 非0表示异常")
    message: str = Field("")
    data: Optional[T] = Field(None)
    debug: Any = Field(None)

    @classmethod
    def ok(cls, data: T):
        return CommonResp(code=0, data=data)  # type: ignore

    @classmethod
    def fail(cls, message: str, debug: Any = None, code: int = 1):
        return CommonResp(code=code, message=message,
                          debug=debug)  # type: ignore
